/*
 * Copyright 2009 Brook Smith brook.smith@paradise.net.nz.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.javagenerate.generator.script.gen;

import java.util.ArrayList;
import java.util.List;

import org.javagenerate.generator.MethodDefinition;
import org.javagenerate.generator.Parameter;
import org.javagenerate.generator.VariableDefinition;
import org.javagenerate.generator.script.ClassDef;
import org.javagenerate.generator.script.GenBeanDefinition;

public class GwtEdit extends ClassDef {

	public void setGenClassName(String genClassName) {
		this.genClassName = genClassName;
	}

	public void setGenServiceAsync(String genServiceAsync) {
		this.genServiceAsync = genServiceAsync;
	}

	public GwtEdit(String classname, String patternName) {
		super(classname, patternName);
		getImports().add("com.google.gwt.core.client.GWT");
		getImports().add("com.google.gwt.user.client.ui.TextBox");
		getImplements().add("IPageContainer");
	}
	

	class ScreenField {
		String type;
		String name;
		GenBeanDefinition beanDefinition;
		String displayName;
		public ScreenField(String type, GenBeanDefinition beanDefinition, String name, String displayName) {
			super();
			this.type = type;
			this.name = name;
			this.displayName = displayName;
			this.beanDefinition = beanDefinition;
		}
	}
	
	private List<ScreenField> screenFields = new ArrayList<ScreenField>();
	private String genClassName;
	private String genServiceAsync;
	private String genService;

	public void setGenService(String genService) {
		this.genService = genService;
	}

	@Override
	public void addBeanDefiniation(GenBeanDefinition beanDefinition) {
		// rearrange
		if (beanDefinition.getType().equals("String")) {
			screenFields.add(new ScreenField("TextBox", beanDefinition, beanDefinition.getName() + "Field", beanDefinition.getName()));
		}
	}
	
	@Override
	public String getExtends() {
		return "BaseEdit<"+ genClassName +", "+ genServiceAsync +">";
	}
	
	private MethodDefinition getToScreen() {
		MethodDefinition toScreen = new MethodDefinition("void", "toScreen");
		toScreen.addParameter(new Parameter("dto", genClassName));

		String content = "this.dto = dto;\n";
		for (ScreenField screenField : screenFields) {
			content += screenField.name + ".setText(dto." + screenField.beanDefinition.getAccessorName() + "());\n";  
		}
	
		toScreen.setContent(content);
		return toScreen;
	}

	private MethodDefinition getInit() {
		MethodDefinition init = new MethodDefinition("void", "init");
		init.setAnnotation("@Override");
		
		String content = "dto = new " + genClassName + "();\n";
		for (ScreenField screenField : screenFields) {
			content += screenField.name + " = new " + screenField.type + "();\n";  
		}
	
		content += "loadEntryFields();\nsuper.init();";
		init.setContent(content);
		return init;
	}
	
	
	
	private MethodDefinition getFromScreen() {
		MethodDefinition fromScreen = new MethodDefinition(genClassName, "fromScreen");
		String content = "validate();\n";
		for (ScreenField screenField : screenFields) {
			content += "dto." + screenField.beanDefinition.getMutatorName() + "("+ screenField.name +".getText());\n";  
		}
		content += "return dto;\n";
	
		fromScreen.setContent(content);
		return fromScreen;
	}
	
	private MethodDefinition getLoadEntryFields() {
		MethodDefinition loadEntryFields = new MethodDefinition("void", "loadEntryFields");
		String content = "";
		for (ScreenField screenField : screenFields) {
			content += "addEntry(\"" + screenField.displayName + "\", "+ screenField.name +");\n";  
		}
		loadEntryFields.setContent(content);
		return loadEntryFields;
	}
	
	@Override
	public void preWrite() {
		super.preWrite();

		addMethod(getInit());
		addMethod(getToScreen());
		addMethod(getFromScreen());
		addMethod(getLoadEntryFields());
		
		addLocalVariable(new VariableDefinition("private", genClassName, "dto", null));
		
		for (ScreenField screenField : screenFields) {
			addLocalVariable(new VariableDefinition("private", screenField.type, screenField.name, null));
		}
	}
	
/*
  	TextBox nameField = new TextBox();
	TextBox addressField = new TextBox();

	protected void toScreen(GenPersonDef personDef) {
		nameField.setText(personDef.getFirstname());
		addressField.setText(personDef.getAddress());
	}

	protected void validate() {
		
	}

	protected GenPersonDTO fromScreen() {
		validate();
		GenPersonDTO person = new GenPersonDTO();
	
		person.setFirstname(nameField.getText());
		person.setAddress(addressField.getText());
		return person;
	}

	protected void loadEntryFields() {
		addEntry("Name", nameField);
		addEntry("Adress", addressField);
	}
	
	
	@Override
	protected GenPersonServiceAsync getService() {
		return GWT.create(GenPersonService.class);
	}

	@Override
	protected void clear() {
		toScreen(new GenPersonDTO());
	}

 * 	
 */

}
